﻿#coding=utf-8
#Boa:Dialog:DlgTest

import wx

from product import *

def create(parent):
    return DlgTest(parent)

[wxID_DLGTEST, wxID_DLGTESTBTNACCOUNTDETAILLOAD, wxID_DLGTESTBTNACCOUNTFUNDID, 
 wxID_DLGTESTBTNCHECKFUNDVOUCHER, wxID_DLGTESTBTNDLVY, wxID_DLGTESTSTATICBOX1, 
 wxID_DLGTESTSTATICBOX2, 
] = [wx.NewId() for _init_ctrls in range(7)]

class DlgTest(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGTEST, name=u'DlgTest', parent=prnt,
              pos=wx.Point(265, 213), size=wx.Size(704, 493),
              style=wx.DEFAULT_DIALOG_STYLE, title='Dialog1')
        self.SetClientSize(wx.Size(696, 459))

        self.btnDlvy = wx.Button(id=wxID_DLGTESTBTNDLVY,
              label=u'\u66f4\u65b0dlvyid', name=u'btnDlvy', parent=self,
              pos=wx.Point(32, 48), size=wx.Size(96, 24), style=0)
        self.btnDlvy.Bind(wx.EVT_BUTTON, self.OnBtnDlvyButton,
              id=wxID_DLGTESTBTNDLVY)

        self.btnAccountFundID = wx.Button(id=wxID_DLGTESTBTNACCOUNTFUNDID,
              label=u'Account\u4e2dfundId\u6539None', name=u'btnAccountFundID',
              parent=self, pos=wx.Point(32, 80), size=wx.Size(152, 24),
              style=0)
        self.btnAccountFundID.Bind(wx.EVT_BUTTON, self.OnBtnAccountFundIDButton,
              id=wxID_DLGTESTBTNACCOUNTFUNDID)

        self.staticBox1 = wx.StaticBox(id=wxID_DLGTESTSTATICBOX1,
              label=u'\u4fee\u6539', name='staticBox1', parent=self,
              pos=wx.Point(24, 16), size=wx.Size(200, 344), style=0)

        self.staticBox2 = wx.StaticBox(id=wxID_DLGTESTSTATICBOX2,
              label=u'\u6821\u9a8c', name='staticBox2', parent=self,
              pos=wx.Point(312, 24), size=wx.Size(200, 336), style=0)

        self.btncheckFundVoucher = wx.Button(id=wxID_DLGTESTBTNCHECKFUNDVOUCHER,
              label=u'\u6821\u9a8c\u4ed8\u6b3e\u5355\u8d44\u91d1\u5206\u914d',
              name=u'btncheckFundVoucher', parent=self, pos=wx.Point(328, 48),
              size=wx.Size(136, 24), style=0)
        self.btncheckFundVoucher.Bind(wx.EVT_BUTTON,
              self.OnBtncheckFundVoucherButton,
              id=wxID_DLGTESTBTNCHECKFUNDVOUCHER)

        self.btnAccountDetailLoad = wx.Button(id=wxID_DLGTESTBTNACCOUNTDETAILLOAD,
              label=u'\u8bbe\u7f6e\u8d44\u91d1\u53d8\u52a8\u5bfc\u5165\u6807\u5fd7',
              name=u'btnAccountDetailLoad', parent=self, pos=wx.Point(32, 112),
              size=wx.Size(152, 24), style=0)
        self.btnAccountDetailLoad.Bind(wx.EVT_BUTTON,
              self.OnBtnAccountDetailLoadButton,
              id=wxID_DLGTESTBTNACCOUNTDETAILLOAD)

    def __init__(self, parent):
        self._init_ctrls(parent)

          
    def OnBtnDlvyButton(self, event):
        dlg = wx.MessageDialog(self, u'Begine Test?', u'提示', wx.YES_NO|wx.ICON_QUESTION)
        if not(dlg.ShowModal() == wx.ID_YES):
            return

        for saleorder in CDataSaleOrder.getObjList():
            dlvyAddr = saleorder.getValue(saleorder.COL_DLVYADDR)
            if dlvyAddr == '':
                continue
            
            dlvyObj = CDataDelivery.getFirstInstance({CDataDelivery.COL_NAME:dlvyAddr})
            dlvyId = dlvyObj.getValue(dlvyObj.COL_ID)
            saleorder.setValue(saleorder.COL_DLVYID, dlvyId)
            saleorder.update()
            
        dlg = wx.MessageDialog(self, u'Test Over!', u'提示')
        dlg.ShowModal()


    def OnBtnAccountFundIDButton(self, event):
        dlg = wx.MessageDialog(self, u'Begine Test?', u'提示', wx.YES_NO|wx.ICON_QUESTION)
        if not(dlg.ShowModal() == wx.ID_YES):
            return
        
        for detail in CDataAccountDetail.getObjList():
            if detail.getValue(detail.COL_FUNDID) == '':
                detail.setValue(detail.COL_FUNDID, None)
                detail.update()
        
        dlg = wx.MessageDialog(self, u'Test Over!', u'提示')
        dlg.ShowModal()

    def checkFundVoucher(self, type):
        '''fund_customer中的voucherId不能设置约束时，怀疑出现问题，增加检测'''
        if type == 1:
            clsFund = CRelFundCustomer
            clsOrder = CDataSaleOrder
            clsVoucher = CDataVoucher
        elif type == 2:
            clsFund = CRelFundSupplier
            clsOrder = CDataPurOrder
            clsVoucher = CDataVoucherSupplier
        
        list = clsFund.fetchRows()
        for billId,voucherId,money in list:
            billObj = clsOrder.findObj(billId)
            voucherObj = clsVoucher.findObj(voucherId)
            if not billObj or not voucherObj:
                print (u'查找不到对象', billId, voucherId)
                
            if billObj.getValue(billObj.COL_PEERID) != voucherObj.getValue(voucherObj.iPeerId):
                print (u'peerId不一致', billId, voucherId)
        
            # todo - voucher分配的金额不能大于收款金额
            # todo - bill获得的金额不能大于订单金额
            
            # TODO - accountdetail对应fundID金额必须一致
                
    def OnBtncheckFundVoucherButton(self, event):
        '''fund_customer中的voucherId不能设置约束时，怀疑出现问题，增加检测'''
        dlg = wx.MessageDialog(self, u'Begine Test?', u'提示', wx.YES_NO|wx.ICON_QUESTION)
        if not(dlg.ShowModal() == wx.ID_YES):
            return
        
        print '----------checkFundVoucher start-------------'
        self.checkFundVoucher(1)
        self.checkFundVoucher(2)    
        #self.voucher2deail(6)
        print '----------checkFundVoucher  over-------------'
            
        dlg = wx.MessageDialog(self, u'Test Over!', u'提示')
        dlg.ShowModal()    

    def OnBtnAccountDetailLoadButton(self, event):
        dlg = wx.MessageDialog(self, u'Begine Test?', u'提示', wx.YES_NO|wx.ICON_QUESTION)
        if not(dlg.ShowModal() == wx.ID_YES):
            return
        
        for detail in CDataAccountDetail.getObjList():
            if u'导入自' in detail.getValue(detail.COL_SYSMEMO):
                detail.setValue(detail.COL_LOADIN, 1)
                detail.update()
        
        dlg = wx.MessageDialog(self, u'Test Over!', u'提示')
        dlg.ShowModal()
            
    def voucher2deail(self, accountId):
        for voucherObj in CDataVoucher.getObjList():
            if voucherObj.getValue(voucherObj.COL_ACCOUNTID) == accountId: # 信用社ID
          
                voucherId = voucherObj.getValue(voucherObj.COL_ID)
                
                if CDataAccountDetail.getFirstInstance({CDataAccountDetail.COL_FUNDID:voucherId}):
                    continue
                
                data = {CDataAccountDetail.COL_ACCOUNTID:accountId,
                CDataAccountDetail.COL_DATE:voucherObj.getValue(voucherObj.COL_DATE),
                CDataAccountDetail.COL_MONEY:voucherObj.getValue(voucherObj.COL_MONEY),
                CDataAccountDetail.COL_FUNDID:voucherId,
                #CDataAccountDetail.COL_MEMO:self.tcPeer.GetValue(),
                CDataAccountDetail.COL_SYSMEMO:u'从收款单转换而得',
                CDataAccountDetail.COL_TIME:datetime.datetime.now(),
                CDataAccountDetail.COL_SUBJECT:1}
            
                
                obj = CDataAccountDetail.factory()
                obj.setMulFieldVal(data)
                obj.insert()
    
    